<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-03-08
 * Time: 11:13
 */
namespace Admin\Controller;

class AuthController extends  BaseController{




    public  function  index(){
        $this->display("Auth/auth_rule_index");
    }
    /**
     * 规则列表
     */
    public  function auth_rule_index(){
        $this->display();
    }

    /**
     * 规则列表jajax
     */
    public  function auth_rule_index_list(){
        $auth_ruleModel=M("auth_rule");
        $parent_id=I("request.parent_id",0);
        $title=I("request.title");
        $where["types"]=0;
        $where["parent_id"]=$parent_id;
        if(!empty($title)){
            $where["title"]=array("like","%$title%");
        }

        $_where_order_field['where']=$where;
        $_where_order_field['order']="sort desc";
        $_where_order_field['id']="order_list_item_content";
        $list=$this->_getListsFromModel("auth_rule",$_where_order_field);
        $this->assign("treeList",$list);
        $this->assign("parent_id",$parent_id);
        $this->assign("title",$title);
        $this->display("Auth/auth_rule_index_list");
    }


    public  function  auth_rule_add(){
           $is_add=intval(I("request.is_add"));
            $this->assign("is_add",$is_add);
            if($is_add==0){
                $this->auth_rule_add_ok();
            }else if($is_add==1){
                $this->auth_rule_update();
            }
    }

    /**
     * 添加
     */
    public  function  auth_rule_add_ok(){
        if(IS_POST){
            $data["name"]=I("request.name");
            $data["title"]=I("request.title");
            $data["status"]=I("request.status");
            $data["parent_id"]=I("request.parent_id");
            $data["sort"]=I("request.sort");
            $data["type"]=1;
            $data["types"]=0;//0表示admin后台，1表示suppliers供应商后台
            $data["is_new"]=I("request.is_new");
            $data["is_display"]=I("request.is_display");
            $auth_ruleModel=M("auth_rule");
            $rowid=$auth_ruleModel->data($data)->add();
            if($rowid){
                if(is_numeric($data["parent_id"])&&intval($data["parent_id"])!=0){
                    $parentEntity=$auth_ruleModel->where(array("id"=>$data["parent_id"]))->find();
                    $updateData["class_parent_id"]=$parentEntity["class_parent_id"].",{".$rowid."}";
                }else{
                    $updateData["class_parent_id"]="{".$rowid."}";
                }
                $dataWhere["id"]=$rowid;
                $auth_ruleModel->where($dataWhere)->save($updateData);

                $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=    art.dialog.opener;
                            win.load();
                            art.dialog.close();
                },2000);

           </script>";

                $this->success("添加成功！".$exe_scriptstr);
            }else{
                $this->error("添加失败！");
            }

        }else{
            $auth_ruleModel= M("auth_rule");
            $authDataList=$auth_ruleModel->where(array("types"=>0))->order("sort desc")->select();
            $treeList=array();
            self::tree($authDataList,0,1,$treeList);
            $parent_id= I("request.parent_id",0);
            $this->assign("parent_id",$parent_id);
            $this->assign("parentlist",$treeList);

            $this->display();
        }

    }

    /**
     * 修改
     */
    public  function  auth_rule_update(){
        if(IS_POST){
            $id=I("request.id");
            $data["name"]=I("request.name");
            $data["title"]=I("request.title");
            $data["status"]=I("request.status");
            $parent_id=I("request.parent_id");
            if(!empty($parent_id)&&is_numeric($parent_id)){
                $data["parent_id"]=$parent_id;
            }
            $data["is_new"]=I("request.is_new");
            $data["sort"]=I("request.sort");
            $data["is_display"]=I("request.is_display");
            $data["types"]=0;//0表示admin后台，1表示suppliers供应商后台
            $data["type"]=1;
            $auth_ruleModel=M("auth_rule");
            $rowid=$auth_ruleModel->where(array("id"=>$id))->save($data);
            if($rowid>0||$rowid===0){
                $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=    art.dialog.opener;
                            win.load();
                            art.dialog.close();
                },2000);

           </script>";

                $this->success("修改成功！".$exe_scriptstr);
            }else{
                $this->success("修改失败！");
            }
        }else{
            $auth_ruleModel= M("auth_rule");
            $authDataList=$auth_ruleModel->where(array("types"=>0))->order("sort desc")->select();
            $treeList=array();
            self::tree($authDataList,0,1,$treeList);
            $parent_id= I("request.parent_id",0);
            $this->assign("parent_id",$parent_id);
            $this->assign("parentlist",$treeList);
            $id=I("request.id");
            $authEntity=$auth_ruleModel->where(array("id"=>$id))->find();
            $this->assign("m",$authEntity);
            $this->assign("is_add",I("request.is_add"));
            $this->assign("status",$authEntity['status']);
            $this->assign("is_new",$authEntity['is_new']);
            $this->assign("is_display",$authEntity['is_display']);
            $this->display();
        }

    }


    /**
     * 无限级分类
     * @access public
     * @param Array $data     //数据库里获取的结果集
     * @param Int $pid
     * @param Int $count       //第几级分类
     * @return Array $treeList
     */
     public static function tree(&$data,$pid = 0,$count = 1,&$treeList=array()) {
        foreach ($data as $key => $value){
            if($value['parent_id']==$pid){
                $value['count'] = $count;
                $treeList []=$value;
                self::tree($data,$value['id'],$count+1,$treeList);
            }
        }

    }


    public  function is_auth_rule_name(){
        $param=I("request.param");
        $data["name"]=$param;
        $data["types"]=0;
        $countrow=M("auth_rule")->where($data)->count();
        if($countrow>0){
            $this->ajaxReturn(array("info"=>"菜单地址已经存在！","status"=>"n"));
        }else{
            $this->ajaxReturn(array("info"=>"菜单地址可以使用！","status"=>"y"));
        }
    }


    public  function delete_ok(){
          $id=I("request.id");
          $auth_ruleModel=M("auth_rule");
          $auth_ruleModel->where(array("class_parent_id"=>array("like","%$id%"),"types"=>0))->delete();
          $auth_ruleModel->where(array("id"=>$id,"types"=>0))->delete();
          $this->success("删除成功！");
    }


    /**
     * zu
     */
    public function auth_group_index(){

        $this->display();
    }

    public function auth_group_index_list(){
        $_where_order_field["order"]="sort desc";
        $where=array();
        $title=I("request.title");
        if(!empty($title)){
            $where["title"]=array("like","%$title%");
        }
        $where["types"]=0;
        $_where_order_field["where"]=$where;
        $_where_order_field['id']="order_list_item_content";
        $_dataList= $this->_getListsFromModel("auth_group",$_where_order_field);
        $this->assign("datalist",$_dataList);
        $this->display();
    }

    public function   auth_group_add(){
        $is_add=I("request.is_add");
        if($is_add==0){
            $this->auth_group_add_ok();
        }else if($is_add==1){
            $this->auth_group_update();
        }
    }


    public  function  auth_group_add_ok(){
        if(IS_POST){
            $data["title"]=I("request.title");
            $data["status"]=I("request.status");
            $data["sort"]=I("request.sort");
            $data["rules"]=I("request.rules");
            $data["remark"]=I("request.remark");
            $data["types"]=0;
            $auth_groupModel=M("auth_group");
            $rowcount=$auth_groupModel->data($data)->add();
            if($rowcount){
                $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=    art.dialog.opener;
                            win.load();
                            art.dialog.close();
                },2000);

           </script>";

                $this->success("添加成功！".$exe_scriptstr);
            }else{
                $this->success("添加失败！");
            }
        }else{

            $is_add=I("request.is_add");
            $this->display();
        }
    }

    public  function  auth_group_update(){
        if(IS_POST){
            $data["title"]=I("request.title");
            $data["status"]=I("request.status");
            $data["sort"]=I("request.sort");
            $data["rules"]=I("request.rules");
            $data["remark"]=I("request.remark");
            $data["types"]=0;
            $auth_groupModel=M("auth_group");
            $id=I("request.id");
            $rowcount=$auth_groupModel->where(array("id"=>$id))->save($data);
            if($rowcount!==false){
                $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=    art.dialog.opener;
                            win.load();
                            art.dialog.close();
                },2000);

           </script>";

                $this->success("编辑成功！".$exe_scriptstr);
            }else{
                $this->success("编辑失败！");
            }
        }else{
            $auth_groupModel=M("auth_group");
            $id=I("request.id");
            $authEntity=$auth_groupModel->where(array("id"=>$id,"types"=>0))->find();
            $this->assign("m",$authEntity);
            $this->assign("status",$authEntity['status']);
            $this->assign("is_add",I("request.is_add"));
            $this->display();
        }
    }

    public  function  auth_group_delete(){
          $id=I("request.id");
        $auth_groupModel=M("auth_group");
        $auth_groupModel->where(array('id'=>$id,"types"=>0))->delete();
        M("auth_group_access")->where(array("group_id"=>$id,"types"=>0))->delete();
        $this->success("删除成功");
    }


    public  function auth_group_rules_ajax(){
        $id=I("request.id");
        $auth_ruleList=M("auth_rule")->field("name as urls,title as name,id,parent_id as pid")->where(array("types"=>0))->order("sort desc")->select();

        if(!empty($id)){
            $authGroupEntity=M("auth_group")->where(array("id"=>$id,"types"=>0))->find();
             $rules=$authGroupEntity["rules"];
            if(!empty($rules)){
                $rulesArray=explode(",",$rules);
                foreach($auth_ruleList as $key=>$value){
                    $auth_ruleList[$key]["pId"]=$value["pid"];
                    $auth_ruleList[$key]["checked"]=in_array($value["id"],$rulesArray);
                }
                $this->ajaxReturn($auth_ruleList);
            }else{
                foreach($auth_ruleList as $key=>$value){
                    $auth_ruleList[$key]["pId"]=$value["pid"];
                }
                $this->ajaxReturn($auth_ruleList);
            }
        }else{
            foreach($auth_ruleList as $key=>$value){
                $auth_ruleList[$key]["pId"]=$value["pid"];
            }
            $this->ajaxReturn($auth_ruleList);
        }


    }

    //授权
    public  function auth_group_access(){
         if(IS_POST){
             $uid=I("request.user_id");
             $group_access_id=I("request.group_access_id");
             if(!empty($group_access_id)){
                 M("auth_group_access")->where(array("uid"=>$uid,"types"=>0))->delete();
                 $bathInsertData=array();
                 foreach($group_access_id as $key=>$value){
                      $bathInsertData[]=array("uid"=>$uid,"group_id"=>$value,'types'=>0);
                 }
                 M("auth_group_access")->addAll($bathInsertData);
                 $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=    art.dialog.opener;
                            win.load();
                            art.dialog.close();
                },2000);

            </script>";

                 $this->success("授权成功！".$exe_scriptstr);
             }else{
                 $this->error("授权失败！");
             }


         }else{
             $uid=I("request.uid");
             $userEntity=M("admin_user")->where(array("user_id"=>$uid))->find();
             $this->assign("user",$userEntity);
             $this->display();
         }
    }

    public  function auth_group_access_load(){
        $uid=I("request.uid");
        $authGroupList= M("auth_group")->where(array("types"=>0))->order("sort desc")->select();
        $auth_group_accessList=M("auth_group_access")->where(array("uid"=>$uid,"types"=>0))->select();
        foreach($authGroupList as $key=>$value){
            $authGroupList[$key]['selectedcheck']=false;
            foreach($auth_group_accessList as $authkey=>$authValue){
                  if($value["id"]==$authValue["group_id"]){
                      $authGroupList[$key]['selectedcheck']=true;
                      break;
                  }
            }
        }
        $this->assign("grouplist",$authGroupList);
        $this->display();
    }


    //获取上一级的父级id
    public  function  return_up(){
        $parent_id=I("request.parent_id");
        if(!empty($parent_id)&&is_numeric($parent_id)&&$parent_id>0){
            $authRuleEntity= M("auth_rule")->where(array("types"=>0,"id"=>$parent_id))->find();
           $return_up_id= $authRuleEntity["parent_id"];
            $this->ajaxReturn(array("status"=>1,"info"=>$return_up_id));
        }else{
            $this->ajaxReturn(array("status"=>0,"info"=>"亲，已经没有页面可以返回了！"));
        }
    }

}
?>